package com.zxk.service.impl;

import com.sun.jna.platform.win32.WinDef;
import com.zxk.constant.JNAConstants;
import com.zxk.constant.ParamConstants;
import com.zxk.constant.PicConstants;
import com.zxk.enums.Coord;
import com.zxk.service.AbstractMyTask;
import com.zxk.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 帮派任务
 *
 * @author: 舟小亢
 * @date: 2023-9-30 12:58:17
 */
public class GangTaskImpl extends AbstractMyTask {

    private static Logger LOG = LoggerFactory.getLogger(GangTaskImpl.class);

    public GangTaskImpl(WinDef.HWND hwnd_0, WinDef.HWND hwnd_1, WinDef.HWND hwnd_2) {
        super(hwnd_0, hwnd_1, hwnd_2);
    }

    @Override
    public String getTaskName() throws Exception {
        return "帮派贡献-青龙";
    }

    @Override
    public boolean execute() throws Exception {
        LOG.info("【帮派任务】开始！");

        // 检查任务-是否为青龙,不是的话先放弃已有任务
        if (isGreenDragon()) {
            LOG.info("【帮派任务】青龙任务已接，直接开始！");
            // 摁一次取消任务界面
            KeyUtil.kClick(hwnd_1, JNAConstants.VK_ESCAPE);
            return true;
        } else {
            LOG.info("【帮派任务】当前任务非青龙，放弃重新接！");
            // 确认任务是否存在
            if (FuzzyPicUtil.findPic(hwnd_0, PicConstants.帮派任务_常规任务_帮派任务, 3).isFinded()) {
                // 放弃任务，重新接
                FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.公共_放弃任务, 3);
                FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.公共_确定, 3);
            }
            // 摁一次取消任务界面
            KeyUtil.kClick(hwnd_1, JNAConstants.VK_ESCAPE);
        }

        // 找到活动并打开
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.公共_活动)) {
            return false;
        }

        // 找到日常活动并点击
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.公共_日常活动)) {
            return false;
        }

        // 找到帮派任务并参加
        MouseUtil.dragMove(hwnd_1, Coord.ACTIVITY_WHELL.getX(), Coord.ACTIVITY_WHELL.getY(), 0, 1000);

        for (int i = 0; i < ParamConstants.UNIFY_TIMES; i++) {
            pic = FuzzyPicUtil.findPic(hwnd_0, PicConstants.帮派任务_任务名字, 2);
            if (pic.isFinded() && FuzzyPicUtil.findPic(hwnd_0, PicConstants.帮派任务_图标, 2).isFinded()) {
                break;
            }
            // 如果没找到下拉滑动屏幕寻找图标
            MouseUtil.dragMove(hwnd_1, Coord.ACTIVITY_WHELL.getX(), Coord.ACTIVITY_WHELL.getY(), 0, -100);

            // 一直没找到
            if (i == ParamConstants.UNIFY_TIMES - 1) {
                LOG.error("【帮派任务】未找到帮派任务，任务失败！");
                return false;
            }
        }
        MouseUtil.lClickRandom(hwnd_1,
                pic.getPicX() + Coord.ATTEND_TASK_DXY.getX() - 45,
                pic.getPicY() + Coord.ATTEND_TASK_DXY.getY() + 30);

        // 点击“我知道了”
        FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.帮派任务_我知道了, 3);

        // 判断是否进入帮派地图
        if (!FuzzyPicUtil.findPic(hwnd_0, PicConstants.帮派任务_帮派地图, 5).isFinded()) {
            LOG.error("【帮派任务】已完成或者未加入帮派！");
            return false;
        }

        // 领取帮派任务
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.帮派任务_领取)) {
            return false;
        }

        // 摁一次取消任务界面
        KeyUtil.kClick(hwnd_1, JNAConstants.VK_ESCAPE);

        return true;
    }

    @Override
    public boolean checkTask() throws Exception {
        for (int i = 0; i < 60; i++) {
            LOG.info("【帮派任务】开始检查任务完成状态！第[{}]轮", i + 1);

            if (i != 0) {
                // 初始化状态
                if (!goHome()) {
                    continue;
                }
                // 再次执行
                if (!execute()) {
                    continue;
                }
            }

            // 滚动任务栏进行青龙任务
            MouseUtil.dragMove(hwnd_1, Coord.TASK_LIST_WHELL.getX(), Coord.TASK_LIST_WHELL.getY(), 0, 1000);

            // 执行青龙任务
            if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.帮派任务_任务列表青龙, 3)) {
                continue;
            }

            // 循环判断
            for (int j = 0; j < 5; j++) {
                LOG.info("【帮派任务】循环检查任务第[{}]轮，任务中第[{}]次", i + 1, j + 1);
                // 执行青龙任务
                FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.帮派任务_任务列表青龙, 2);

                // 点击“我知道了”
                FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.帮派任务_我知道了, 2);

                // 购买
                pic = FuzzyPicUtil.findPicM(hwnd_0, new String[]{PicConstants.帮派任务_购买, PicConstants.帮派任务_购买1}, 3);
                if (pic.isFinded()) {
                    MouseUtil.lClickRandom(hwnd_1, pic.getPicX(), pic.getPicY());
                }
                // 继续帮派任务
                FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.帮派任务_继续帮派任务, 3,0.8);

                // 点击“我知道了”
                FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.帮派任务_我知道了, 2);
            }
        }
        LOG.info("【帮派任务】帮派任务已完成！");
        return true;
    }


    /**
     * 是否为青龙任务
     *
     * @return
     */
    private boolean isGreenDragon() throws Exception {

        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.公共_任务, 3)) {
            return false;
        }
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.公共_主线剧情, 3)) {
            return false;
        }
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.公共_常规任务, 3)) {
            return false;
        }
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.帮派任务_常规任务_帮派任务, 3)) {
            return false;
        }
        if (!FuzzyPicUtil.findPic(hwnd_0, PicConstants.帮派任务_帮派青龙, 3).isFinded()) {
            return false;
        }
        return true;
    }

}
